package org.example.infrastructure.repository.orm.database;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.domain.model.entity.url.UrlShortener;
import org.example.domain.model.entity.url.req.UrlShortenerSelectReq;
import org.example.domain.model.entity.url.resp.QueryGroupByCountResp;
import org.example.domain.model.entity.url.vo.UrlShortenerUpdateVo;
import org.example.domain.model.entity.url.vo.UrlShortenerVo;
import org.example.domain.repository.UrlShortenerRepository;
import org.example.infrastructure.persistence.orm.UrlShortenerMapper;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

@Component
@Slf4j
@RequiredArgsConstructor
public class MybatisUrlShortenerRepository implements UrlShortenerRepository {
    @Resource
    private UrlShortenerMapper urlShortenerMapper;

    @Override
    public void insertUrlShortener(UrlShortener urlShortener) {
        urlShortenerMapper.insert(urlShortener);
    }

    @Override
    public Optional<UrlShortenerVo> selectUrlShortener(String fullShortUrl) {
        return urlShortenerMapper.selectUrlShortener(fullShortUrl);
    }

    @Override
    public void updateUrlShortEnerByFullShortUrl(UrlShortenerUpdateVo urlShortenerUpdateVo, String fullShortUrl, String gid) {
        urlShortenerMapper.updateUrlShortEnerByFullShortUrl(urlShortenerUpdateVo, fullShortUrl, gid);
    }

    @Override
    public PageInfo<UrlShortener> selectByPage(UrlShortenerSelectReq urlShortenerSelectReq) {
        PageHelper.startPage(urlShortenerSelectReq.getPageNum(), urlShortenerSelectReq.getPageSize());
        List<UrlShortener> urlShorteners = urlShortenerMapper.selectByPage(urlShortenerSelectReq);
        return new PageInfo(urlShorteners);
    }

    @Override
    public List<QueryGroupByCountResp>  queryGroupByCount(List<String> gidList) {
        return urlShortenerMapper.queryGroupByCount(gidList);
    }

    @Override
    public void incrementStats(String gid, String fullShortUrl, Integer totalPv, Integer totalUv, Integer totalUip) {
        urlShortenerMapper.incrementStats(gid, fullShortUrl, totalPv, totalUv, totalUip);
    }
}
